﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.Instance;
using l1cs.server.serverpackets;

namespace l1cs.server.clientpackets
{
    public class C_AuthLogin : ClientBasePacket
    {
        private const String _C_AUTH_LOGIN = "[C] C_AuthLogin";
        private static Logger _log = Logger.getLogger( typeof( C_AuthLogin ).FullName );

        public C_AuthLogin( byte[] decrypt, ClientThread client )
            : base( decrypt )
        {
            String accountName = readS().ToLower();
            String password = readS();

            String ip = client.getIp();
            String host = client.getHostname();

            _log.finest( "Request AuthLogin from user : " + accountName );

            if ( !Config.ALLOW_2PC )
            {
                if ( LoginController.getInstance().isAlready( ip.ToLower() ) )
                {
                    _log.info( "2PCのログインを拒否しました。account=" + accountName + " host=" + host );
                    client.sendPacket( new S_LoginResult( S_LoginResult.REASON_USER_OR_PASS_WRONG ) );
                    return;
                }
            }

            Account account = new Account();
            if ( account.load( accountName ) == false )
            {
                if ( Config.AUTO_CREATE_ACCOUNTS )
                {
                    account.create( accountName, password, ip, host );
                }
                else
                {
                    _log.warning( "account missing for user " + accountName );
                }
            }
            if ( account == null || !account.validatePassword( password ) )
            {
                client.sendPacket( new S_LoginResult( S_LoginResult.REASON_USER_OR_PASS_WRONG ) );
                return;
            }
            if ( account.isBanned() )
            { // BANアカウント
                _log.info( "BANアカウントのログインを拒否しました。account=" + accountName + " host=" + host );
                client.sendPacket( new S_LoginResult( S_LoginResult.REASON_USER_OR_PASS_WRONG ) );
                return;
            }

            int state = LoginController.getInstance().login( client, account );
            if ( state == LoginController.LOGIN_OK )
            {
                account.updateLastActive(); // 最終ログイン日を更新する
                client.setAccount( account );
                client.sendPacket( new S_LoginResult( S_LoginResult.REASON_LOGIN_OK ) );
                client.sendPacket( new S_CommonNews() );
            }
            else if ( state == LoginController.GAME_SERVER_FULL )
            {
                client.kick();
                _log.info( "接続人数上限に達している為(" + client.getHostname() + ")のログインを拒否し、切断しました。" );
                return;
            }
            else if ( state == LoginController.ACCOUNT_ALREADY_LOGIN )
            {
                client.kick();
                _log.info( "同一IDでの重複接続の為(" + client.getHostname() + ")との接続を強制切断しました。" );
                return;
            }
            else
            {
                return;
            }
        }

        override
        public String getType()
        {
            return _C_AUTH_LOGIN;
        }

    }
}